﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public enum TURN
{
	HUMAN,
	AI1,
	AI2,
};
public class BoardManager : MonoBehaviour {
	public GameObject arrow1;
	public GameObject arrow2;
	public GameObject winPanel;
	public GameObject blurBG;
	public UILabel attWin;
	public UILabel nameAI;
	public int playerWin ;// -1 mean black, 1 mean white, 0 mean draw
	/*game mode*/
	public GameMode gameMode;
	/*score*/
	public UILabel namePlayer1;
	public UILabel namePlayer2;
	public UILabel scorePlayer1;
	public UILabel scorePlayer2;
	public int score1=2;
	public int score2=2;

	public bool isWin=false;
	public float timeforAI;
	bool delayDone1=false;
	bool delayDone2=false;
	float delayTime=0;
	static public TURN curTurn;
	public int[,] cangoBoard=new int[8,8];
	public int[,] oldBoard=new int[8,8];
	public int[,] iBoard=new int[8,8];
	public LineCell[] listCell;
	public LineCell[] listCellCanGo;
	static public Vector2 curID;
	static public bool isClick=false;
	static public int lastValue;
	// Use this for initialization
	void Awake(){
		gameMode=FindObjectOfType<GameMode>();
		Cell[] list=FindObjectsOfType<Cell>();
		foreach(Cell cell in list)
		{
			cell.curColor=0;
		}
		for(int i=0;i<8;i++){
			for(int j=0;j<8;j++){
				iBoard[i,j]=0;
			}
		}
		iBoard[3,4]=1;
		iBoard[4,4]=-1;
		iBoard[3,3]=-1;
		iBoard[4,3]=1;
		for(int i=0;i<8;i++){
			for(int j=0;j<8;j++){
				oldBoard[i,j]=iBoard[i,j];
			}
		}

	}
	void Start () {
		switch(gameMode.mode){
		case 0:
			curTurn=TURN.HUMAN;
			namePlayer1.text="HUMAN";
			namePlayer2.text="AI";
			cangoBoard=checkRule.Test(iBoard,-1);
			for(int i=0;i<8;i++){
				for(int j=0;j<8;j++){
					if(cangoBoard[i,j]==2){
						listCellCanGo[i].gameObject.SetActive(true);
						Cell temp=listCellCanGo[i].getCell(j);
						temp.gameObject.SetActive(true);
						Cell tmp=listCell[i].getCell(j);
						tmp.GetComponent<BoxCollider>().enabled=true;
					}
					else{
						Cell temp=listCellCanGo[i].getCell(j);
						temp.gameObject.SetActive(false);
					}
				}
			}
			break;
		case 1:
			curTurn=TURN.AI1;
			namePlayer1.text="AI1";
			namePlayer2.text="AI2";
			cangoBoard=checkRule.Test(iBoard,1);
			for(int i=0;i<8;i++){
				for(int j=0;j<8;j++){
					if(cangoBoard[i,j]==2){
						listCellCanGo[i].gameObject.SetActive(true);
						Cell temp=listCellCanGo[i].getCell(j);
						temp.gameObject.SetActive(true);
					}
					else{
						Cell temp=listCellCanGo[i].getCell(j);
						temp.gameObject.SetActive(false);
					}
				}
			}
			break;
		}
	}
	
	// Update is called once per frame
	void Update () {
		if(isWin==false){
		#region check win
		switch(playerWin){
		case -1:
			isWin=true;
			print ("WIN -1");
			break;
		case 1:
			isWin=true;
			print ("WIN 1");
			break;
		}
		if(playerWin==0 && score1>2){
			isWin=true;
		}
		#endregion
		#region logic Human vs Com
		if(gameMode.mode==0)
		{
			if(curTurn==TURN.AI1 && delayDone1==false){
				delayTime+=Time.deltaTime;
				if(delayTime>=timeforAI){
					delayTime=0;
					delayDone1=true;
				}
			}
			if(isClick && curTurn==TURN.HUMAN){
				iBoard=checkRule.Danh_x(iBoard,-1,(int)curID.x,(int)curID.y);
				score1=0;
				score2=0;
				for(int i=0;i<8;i++){
					for(int j=0;j<8;j++){
						if(iBoard[i,j]==-1)
						{
							score1++;
							Cell temp=listCell[i].getCell(j);
							Cell tmp=listCellCanGo[i].getCell(j);
							tmp.gameObject.SetActive(false);
							temp.curColor=iBoard[i,j];
							temp.updateSprite();
							temp.playFillAnim=true;
						}
					}
				}
				for(int i=0;i<8;i++){
					for(int j=0;j<8;j++){
						if(iBoard[i,j]==1){
							score2++;
						}
					}
				}
				scorePlayer1.text=score1.ToString();
				scorePlayer2.text=score2.ToString();
				curTurn=TURN.AI1;
				arrow1.SetActive(true);
				arrow2.SetActive(false);
				for(int i=0;i<8;i++){
					for(int j=0;j<8;j++){
						oldBoard[i,j]=iBoard[i,j];
					}
				}
				isClick=false;
				lastValue=0;
				playerWin=checkRule.Ch_Win(iBoard);
				cangoBoard=checkRule.Test(iBoard,1);
				for(int i=0;i<8;i++){
					for(int j=0;j<8;j++){
						if(cangoBoard[i,j]==2){
							listCellCanGo[i].gameObject.SetActive(true);
							Cell temp=listCellCanGo[i].getCell(j);
							temp.gameObject.SetActive(true);
							Cell tmp=listCell[i].getCell(j);
							tmp.GetComponent<BoxCollider>().enabled=true;
						}
						else{
							Cell temp=listCellCanGo[i].getCell(j);
							temp.gameObject.SetActive(false);
							Cell tmp=listCell[i].getCell(j);
							tmp.GetComponent<BoxCollider>().enabled=false;
						}
					}
				}
			}
			if(curTurn==TURN.AI1 && delayDone1){
				curID=checkRule.AI(1,iBoard,gameMode.AI1);
				iBoard=checkRule.Danh_x(iBoard,1,(int)curID.x,(int)curID.y);
				score2=0;
				score1=0;
				for(int i=0;i<8;i++){
					for(int j=0;j<8;j++){
						if(iBoard[i,j]==1)
						{
							score2++;
							Cell temp=listCell[i].getCell(j);
							Cell tmp=listCellCanGo[i].getCell(j);
							tmp.gameObject.SetActive(false);
							temp.curColor=iBoard[i,j];
							temp.updateSprite();
							temp.playFillAnim=true;
							
						}
					}
				}
				for(int i=0;i<8;i++){
					for(int j=0;j<8;j++){
						if(iBoard[i,j]==-1){
							score1++;
						}
					}
				}
				scorePlayer1.text=score1.ToString();
				scorePlayer2.text=score2.ToString();
				curTurn=TURN.HUMAN;
				arrow2.SetActive(true);
				arrow1.SetActive(false);
				for(int i=0;i<8;i++){
					for(int j=0;j<8;j++){
						oldBoard[i,j]=iBoard[i,j];
					}
				}
				isClick=false;
				lastValue=0;
				delayDone1=false;
				playerWin=checkRule.Ch_Win(iBoard);
				cangoBoard=checkRule.Test(iBoard,-1);
				for(int i=0;i<8;i++){
					for(int j=0;j<8;j++){
						if(cangoBoard[i,j]==2){
							listCellCanGo[i].gameObject.SetActive(true);
							Cell temp=listCellCanGo[i].getCell(j);
							temp.gameObject.SetActive(true);
							Cell tmp=listCell[i].getCell(j);
							tmp.GetComponent<BoxCollider>().enabled=true;
						}
						else{
							Cell temp=listCellCanGo[i].getCell(j);
							temp.gameObject.SetActive(false);
							Cell tmp=listCell[i].getCell(j);
							tmp.GetComponent<BoxCollider>().enabled=false;
						}
					}
				}
			}
		}
		#endregion

		#region logic Com vs Com
		if(gameMode.mode==1)
		{
			if(curTurn==TURN.AI1 && delayDone1==false){
				delayTime+=Time.deltaTime;
				if(delayTime>=timeforAI){
					delayTime=0;
					delayDone1=true;
				}
			}
			if(curTurn==TURN.AI2 && delayDone2==false){
				delayTime+=Time.deltaTime;
				if(delayTime>=timeforAI){
					delayTime=0;
					delayDone2=true;
				}
			}
			if(curTurn==TURN.AI1 && delayDone1){
				curID=checkRule.AI(1,iBoard,gameMode.AI1);
				iBoard=checkRule.Danh_x(iBoard,1,(int)curID.x,(int)curID.y);
				score1=0;
				score2=0;
				for(int i=0;i<8;i++){
					for(int j=0;j<8;j++){
						if(iBoard[i,j]==1)
						{
							score1++;
							
							Cell temp=listCell[i].getCell(j);
							temp.curColor=iBoard[i,j];
							temp.updateSprite();
							temp.playFillAnim=true;
							
						}
					}
				}
				for(int i=0;i<8;i++){
					for(int j=0;j<8;j++){
						if(iBoard[i,j]==-1){
							score2++;
						}
					}
				}
				scorePlayer1.text=score1.ToString();
				scorePlayer2.text=score2.ToString();
				curTurn=TURN.AI2;
				arrow1.SetActive(true);
				arrow2.SetActive(false);
				for(int i=0;i<8;i++){
					for(int j=0;j<8;j++){
						oldBoard[i,j]=iBoard[i,j];
					}
				}
				isClick=false;
				lastValue=0;
				delayDone1=false;
				playerWin=checkRule.Ch_Win(iBoard);
				cangoBoard=checkRule.Test(iBoard,-1);
				for(int i=0;i<8;i++){
					for(int j=0;j<8;j++){
						if(cangoBoard[i,j]==2){
							listCellCanGo[i].gameObject.SetActive(true);
							Cell temp=listCellCanGo[i].getCell(j);
							temp.gameObject.SetActive(true);
						}
						else{
							Cell temp=listCellCanGo[i].getCell(j);
							temp.gameObject.SetActive(false);
						}
					}
				}
			}
			if(curTurn==TURN.AI2 && delayDone2){
				curID=checkRule.AI(-1,iBoard,gameMode.AI2);
				iBoard=checkRule.Danh_x(iBoard,-1,(int)curID.x,(int)curID.y);
				score2=0;
				score1=0;
				for(int i=0;i<8;i++){
					for(int j=0;j<8;j++){
						if(iBoard[i,j]==-1)
						{
							score2++;
							Cell temp=listCell[i].getCell(j);
							temp.curColor=iBoard[i,j];
							temp.updateSprite();
							temp.playFillAnim=true;
							
						}
					}
				}
				for(int i=0;i<8;i++){
					for(int j=0;j<8;j++){
						if(iBoard[i,j]==1){
							score1++;
						}
					}
				}
				scorePlayer1.text=score1.ToString();
				scorePlayer2.text=score2.ToString();
				curTurn=TURN.AI1;
				arrow2.SetActive(true);
				arrow1.SetActive(false);
				for(int i=0;i<8;i++){
					for(int j=0;j<8;j++){
						oldBoard[i,j]=iBoard[i,j];
					}
				}
				isClick=false;
				lastValue=0;
				delayDone2=false;
				playerWin=checkRule.Ch_Win(iBoard);
				cangoBoard=checkRule.Test(iBoard,1);
				for(int i=0;i<8;i++){
					for(int j=0;j<8;j++){
						if(cangoBoard[i,j]==2){
							listCellCanGo[i].gameObject.SetActive(true);
							Cell temp=listCellCanGo[i].getCell(j);
							temp.gameObject.SetActive(true);
						}
						else{
							Cell temp=listCellCanGo[i].getCell(j);
							temp.gameObject.SetActive(false);
						}
					}
				}
			}
		
		}
		#endregion
		}
		if(isWin){
			blurBG.SetActive(true);
			winPanel.SetActive(true);
			if(gameMode.mode==0){
				if(playerWin==-1){
					attWin.text="WIN";
				}
				if(playerWin==1){
					attWin.text="LOSE";
				}
			}
			if(gameMode.mode==1){
				attWin.text="WIN";
				if(playerWin==-1){
					nameAI.text="AI2";
				}
				if(playerWin==1){
					nameAI.text="AI1";
				}
			}
		}
		}
}
